﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class ChiTietKhamBenh
    {
        PhongMachTuDBDataContext db;
        public List<CHITIETKHAMBENH> LayDSKhamBenh()
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.CHITIETKHAMBENHs
                    select s;
            return q.ToList();
        }
        public bool ThemChiTietKhamBenh(CHITIETKHAMBENH s)
        {

            try
            {
                db = new PhongMachTuDBDataContext();
                CHITIETKHAMBENH r = new CHITIETKHAMBENH();
                r.MaDonVi = s.MaDonVi;
                r.MaKhamBenh = s.MaKhamBenh;
                r.MaCachDung = s.MaCachDung;
                r.MaThuoc = s.MaThuoc;
                r.SoLuong = s.SoLuong;
        
                db.CHITIETKHAMBENHs.InsertOnSubmit(r);
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool suaChiTietKhamBenh(CHITIETKHAMBENH s)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                CHITIETKHAMBENH r = db.CHITIETKHAMBENHs.Single(id => id.MaChiTietKhamBenh == s.MaChiTietKhamBenh);
                r.MaDonVi = s.MaDonVi;
                r.MaKhamBenh = s.MaKhamBenh;
                r.MaDonVi = s.MaDonVi;
                r.MaThuoc = s.MaThuoc;
                r.SoLuong = s.SoLuong;
                r.MaCachDung = s.MaCachDung;
        
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool xoaChiTietKhamBenh(int MaChiTietKhamBenh)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                var delete = from d in db.CHITIETKHAMBENHs
                             where d.MaChiTietKhamBenh == MaChiTietKhamBenh
                             select d;
                if (delete.Count() > 0)
                {
                    db.CHITIETKHAMBENHs.DeleteOnSubmit(delete.FirstOrDefault());
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
